package com.ssg.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.LinkedList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {


    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button01 = findViewById(R.id.button01);
        TextView viewById = findViewById(R.id.tag1);
        GridLayout gridLayout = findViewById(R.id.gridlayout01);

        context = this;
        LinkedList<Integer> linkedList  = new LinkedList<>();

        button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random random = new Random();
                int value = random.nextInt(10);
                linkedList.add(value);
                if(linkedList.size()>20){
                    linkedList.removeFirst();
                    int childCount = gridLayout.getChildCount();
                    Log.w("MainActivity","==================childCount==========>"+childCount);
                    System.out.println("==================childCount==========>"+childCount);
                    if(childCount>0){
                        gridLayout.removeViews(0,1);
                    }
                }

                button01.setEnabled(false);

                viewById.setText(" ");

                new Thread( new Runnable(){

                    @Override
                    public void run() {
                        for(int i=0;i<=5;i++){
                            viewById.setText(String.valueOf(random.nextInt(10)));
                            try {
                                Thread.sleep(100);
                                button01.setText("("+(5-i)+")");
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }

                        // 切换回主线程更新UI
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                viewById.setText(String.valueOf(value));

                                final TextView textView = new TextView(context);
                                textView.setText(String.valueOf(value));
                                textView.setPadding(5,5,5,5);
                                GridLayout.LayoutParams params = new GridLayout.LayoutParams();
                                params.setMargins(15,15,15,15);
                                textView.setWidth(80);
                                textView.setHeight(160);
                                textView.setTextSize(38);
                                textView.setBackgroundColor(500004);
                                textView.setLayoutParams(params);
                                textView.setGravity(Gravity.CENTER);
                                gridLayout.addView(textView);

                                button01.setText("开始");
                                button01.setEnabled(true);
                            }
                        });
                    }
                }).start();
            }
        });
    }
}

